﻿# Chip8 is a virtual machine designed in 1977 for programming video games.
# Octo is a high level assembler, disassembler and simulator for Chip8.
# Click 'Run' and then press ASD to change attack.
# Click the Octo logo for source, documentation and examples.

:alias bstate v3
:alias groundx v4
:alias groundy v5
:alias control v6
:alias slow v7
:alias px v8
:alias py v9
:alias gstate va
:alias score vb
:alias sexdrive vc

:const bflyminx 8
:const bflymaxx 16
:const bflyminy 3
:const bflymaxy 45
:const maxscore 127

: main
: start
	setup
	drawlogo
	loop
		input
		movefly
		dotest
		updatescore
		sextest
		slow += 1
		if slow > sexdrive begin
			scrollworld
			drawflower
			score += -1
			gameovercheck
			slow := 0
		end
		
    # lock the framerate of this program via the delay timer:
    loop
      vf := delay
      if vf != 0 then
    again
    vf := 3
    delay := vf
	again
	
: setup
	bstate := 0
	groundx := 0
	groundy := 0
	control := 0
	slow := 0
	gstate := 0
	hires
	drawground
	px := bflyminx
	py := bflyminy
	drawfly
	i := tone
	audio
	score := 64
	sexdrive := 6
return

: input
	v0 := OCTO_KEY_W if v0 key then control := 1
  v0 := OCTO_KEY_S if v0 key then control := 2
return

: drawfly
	if bstate == 0 begin
		i := bflyone
		sprite px py 8
	else
		i := bflytwo
		sprite px py 8
	end
return

: movefly
	drawfly
	if control == 1 begin
		py += -1
	else
		py += 2
	end
	v0 := random 0b00000011
	v0 += 254
	py += v0
	v0 := random 0b00000011
	v1 := random 0b00000001
	v0 += 254
	v0 += v1
	px += v0
	if px < bflyminx then px := bflyminx
	if px > bflymaxx then px := bflymaxx
	if py < bflyminy then py := bflyminy
	if py > bflymaxy then py := bflymaxy
	drawfly
return

: flap
	if bstate == 0 begin
		bstate := 1
	else
		bstate := 0
	end
return

: scrollworld
	drawfly
	flap
	plane 3
	scroll-left
	plane 1
	if groundx == 120 begin
		if gstate == 0 begin
			i := ground
			gstate := 1
		else
			gstate := 0
			i := groundtwo
		end
		sprite groundx groundy 8
		groundx += 4
	else
		groundx += 252
	end
	drawfly
return

: drawground
	groundx := 0
	groundy := 56
	loop
		i := ground
		sprite groundx groundy 8
		groundx += 8
		i := groundtwo
		sprite groundx groundy 8
		groundx += 8
	if groundx != 128 then
	again
	groundx := 124
	gstate := 0
return

: drawflower
	if bstate != 1 then return
	v0 := 120
	v2 := random 0b00000011
	if v2 != 0 then return
	v2 := random 0b00000011
	v1 := 48
	i := stem
	loop
    while v2 != 0
    sprite v0 v1 8
    v1 += -8
    v2 += -1
	again
	v1 += -7
	i := flower
	sprite v0 v1 15
	plane 2
	i := goal
	sprite v0 v1 5
	plane 1
return

: dotest
	plane 2
	i := test
	vf := 0
	sprite px py 7
	if vf != 0 begin
		v0 := 4
		score += 8
		if score > maxscore then score := maxscore
		buzzer := v0
	end
	sprite px py 7
	plane 1
return

: updatescore
	v0 := 0
	i := dot
	plane 1
	sprite score v0 1
return

: sextest
	if score > 96 begin
		sexdrive := 2
		return
	end
	if score > 64 begin
		sexdrive := 4
		return
	end
	if score > 32 begin
		sexdrive := 6
		return
	end
	sexdrive := 10
return

: gameovercheck
	if score == 0 then gameover
return

: gameover
	i := badtone
	audio
	v0 := 16
	buzzer := v0
	jump start
return

: drawlogo
i := logo
v0 := 12
v1 := 12
v2 := 32
loop
	sprite v0 v1 0
	i += v2
	v0 += 16
	if v0 == 108 then v1 += 6
	while v0 < 128
again
vf := 64
delay := vf
loop
  vf := delay
  if vf != 0 then
again
return

: bflyone
	0x61 0x92 0x92 0x8C 0x4C 0x9C 0x6C 0x08
	
: bflytwo
	0x21 0x32 0x32 0x2C 0x1C 0x2C 0x1C 0x08
	
: ground
	0xFF 0x80 0x60 0x16 0x09 0xD0 0x20 0xFF
	
: groundtwo
	0xFF 0x43 0x24 0x18 0xA4 0x43 0x44 0xFF
	
: flower
	0x76 0xFF 0xF7 0x5F 0xFA 0xEF 0xFF 0x6E 0x08 0xC8 0x68 0x10 0x10 0x16 0x1C
	
: stem
	0x08 0x08 0xC8 0x68 0x10 0x10 0x16 0x1C

: goal
	0x00 0x00 0x00 0x18 0x18

: test
	0x00 0x00 0x00 0x00 0x1C 0x1C 0x0C
	
: tone
	0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC 0xFC
	
: dot
	0x80
	
: badtone
	0xC9 0x4F 0x53 0xB4 0xC7 0xE2 0xDE 0x77 0x40 0xF7 0x3F 0x0C 0x78 0xC8 0xEE 0x7E
	
: logo
0x00 0x38 0x3F 0xF3 0xFC 0x73 0x70 0x13 0x60 0x01 0x60 0x01 0x60 0x81 0x7F 0x81 0x7E 0x01 0x70 0x01 0x70 0x01 0x70 0x01 0x70 0x01 0x78 0x01 0x78 0x01 0xFC 0x03
0x00 0x00 0xC0 0x00 0xC0 0x00 0x80 0x00 0x80 0x06 0x9E 0x1E 0x8E 0x0E 0x8E 0x0E 0x8E 0x06 0x8C 0x06 0x8C 0x06 0x8C 0x06 0xCE 0x0E 0xC7 0x0E 0xC3 0x1E 0xE1 0xF6
0x38 0x00 0x38 0x70 0x38 0x70 0x30 0x70 0x30 0x60 0x30 0x60 0x3E 0x60 0x70 0x7C 0x30 0xE0 0x30 0x60 0x30 0x60 0x38 0x60 0x3B 0x70 0x1B 0x76 0x0E 0x36 0x00 0x1C
0x00 0x00 0x00 0x00 0x0F 0x1E 0x3F 0x97 0x61 0xC7 0xE0 0x67 0xC0 0x66 0xC0 0xE6 0xFF 0xE6 0xF0 0x06 0xC0 0x06 0x61 0xC6 0x61 0xE7 0x60 0xE7 0x31 0x87 0x0E 0x0F
0x00 0x00 0x00 0x00 0x3F 0x00 0xE3 0x00 0xE7 0x00 0xC7 0x00 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x80 0x00 0xE0 0x00
0x3F 0xC0 0x7F 0xF0 0x0C 0x78 0x0C 0x3C 0x0C 0x1C 0x04 0x1C 0x04 0x38 0x0D 0xF0 0x0F 0xC0 0x3F 0xF0 0x7C 0xF8 0x6C 0x3C 0x60 0x3C 0x60 0x3C 0x70 0x78 0x3F 0xF0
0x00 0xC0 0x71 0xC0 0xF3 0xC0 0xE1 0xC0 0xE0 0xC0 0x60 0xC0 0x70 0xC0 0x79 0xC0 0x3F 0xC0 0x1E 0xC0 0x00 0xC0 0x40 0xC0 0xE0 0xC0 0xE1 0x80 0x3F 0x00 0x1E 0x00